package com.hl.toolkit.admin.controller;

import com.tycoin.AdminConstants;
import com.tycoin.common.bean.BaseResult;
import com.tycoin.common.bean.PageInfo;
import com.tycoin.common.enums.PayModeEnum;
import com.tycoin.common.enums.PaymentStatusEnum;
import com.tycoin.common.utils.BeanInfoUtil;
import com.tycoin.content.bean.ImageBean;
import com.tycoin.news.slaveentity.TycoinNews;
import com.tycoin.news.slaverepository.TycoinNewsRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Controller
@RequestMapping(value = {"/tycoinnews/tycoinnews"})
public class TycoinNewsController extends AbstractImageController {

    
    
    @Autowired
    private TycoinNewsRepository tycoinNewsRepository;

    @RequestMapping(value = {""})
    public String list(Model model, HttpServletRequest request, PageInfo pageInfo) {
        Page<TycoinNews> page = find(request, pageInfo,tycoinNewsRepository);
        model.addAttribute("page", page);
          model.addAttribute("statusEnum", PaymentStatusEnum.values());
 
        return "tycoinnews/tycoinnews/list";
    }

    @RequestMapping(value = {"add"}, method = RequestMethod.GET)
    public String toAdd(Model model) {
        model.addAttribute("statusEnum", PaymentStatusEnum.values());
        model.addAttribute("payModeEnum", PayModeEnum.values());
        return "tycoinnews/tycoinnews/edit";
    }

    @RequestMapping(value = {"add"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult add(@RequestBody ImageBean<TycoinNews> imageBean) {
        return edit(imageBean, null);
    }

    @RequestMapping(value = {"{id}/edit"}, method = RequestMethod.GET)
    public String toEdit(Model model, HttpServletRequest request, @PathVariable("id") Long id) {
        TycoinNews tycoinNews = tycoinNewsRepository.findOne(id);
        model.addAttribute("tycoinNews", tycoinNews);
         return "tycoinnews/tycoinnews/edit";
    }

    @RequestMapping(value = {"{id}/edit"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult edit(@RequestBody ImageBean<TycoinNews> imageBean, @PathVariable("id") Long id) {
        TycoinNews tycoinNews = imageBean.getData();
        String image1Data = imageBean.getImage1Data();
        String image2Data = imageBean.getImage2Data();
        String image3Data = imageBean.getImage3Data();
        String iconData = imageBean.getImage4Data();


        TycoinNews tycoinNewsInfo = null;
        String image1Old = "";
        String image2Old = "";
        String image3Old = "";
        String image4Old = "";
        if (id == null) {
            tycoinNewsInfo = tycoinNews;
        } else {
            tycoinNewsInfo = tycoinNewsRepository.findOne(id);
            tycoinNewsInfo.setUpdateTime(new Date());
            image1Old = tycoinNewsInfo.getNewsImg1();
            image2Old = tycoinNewsInfo.getNewsImg2();
            image3Old = tycoinNewsInfo.getNewsImg3();
            image4Old=tycoinNewsInfo.getIcon();
            BeanInfoUtil.bean2bean(tycoinNews, tycoinNewsInfo, "type,code,name,ename,tag,loveNum,hateNum,greetRate,evaluate,image1,image2,image3,image4,birthday,age,"
                    + "constellation,height,weight,country,area,nation,status,info,clubId,no,position,honor,militaryExploits,ko,surrender,determine,arm,leg");
        }

        String image1 = "";
        String image2 = "";
        String image3 = "";
        String image4 = "";
        if (StringUtils.isNotEmpty(iconData)) {
            image4 = upload(AdminConstants.MODULE_RESOURCE_NEWS, AdminConstants.RESOURCE_TYPE_POSTER, image1Data);
            tycoinNewsInfo.setIcon(image4);
        }
        if (StringUtils.isNotEmpty(image1Data)) {
            image1 = upload(AdminConstants.MODULE_RESOURCE_NEWS, AdminConstants.RESOURCE_TYPE_POSTER, image1Data);
            tycoinNewsInfo.setNewsImg1(image1);
        }
        if (StringUtils.isNotEmpty(image2Data)) {
            image2 = upload(AdminConstants.MODULE_RESOURCE_NEWS, AdminConstants.RESOURCE_TYPE_POSTER, image2Data);
            tycoinNewsInfo.setNewsImg2(image2);
        }
        if (StringUtils.isNotEmpty(image3Data)) {
            image3 = upload(AdminConstants.MODULE_RESOURCE_NEWS, AdminConstants.RESOURCE_TYPE_POSTER, image2Data);
            tycoinNewsInfo.setNewsImg3(image3);
        }
        tycoinNewsRepository.save(tycoinNewsInfo);

        if (!StringUtils.trimToEmpty(image1Old).equals(StringUtils.trimToEmpty(tycoinNews.getNewsImg1()))) {
            // deleteOldResource(image1Old);
        }

        if (!StringUtils.trimToEmpty(image2Old).equals(StringUtils.trimToEmpty(tycoinNews.getNewsImg2()))) {
            // deleteOldResource(image2Old);
        }
        
        if (!StringUtils.trimToEmpty(image3Old).equals(StringUtils.trimToEmpty(tycoinNews.getNewsImg3()))) {
            // deleteOldResource(image2Old);
        }
        if (!StringUtils.trimToEmpty(image4Old).equals(StringUtils.trimToEmpty(tycoinNews.getNewsImg3()))) {
            // deleteOldResource(image2Old);
        }


        return new BaseResult();
    }

    @RequestMapping(value = {"{id}/delete"}, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult delete(@PathVariable("id") Long id) {
    	tycoinNewsRepository.delete(id);
        return new BaseResult();
    }

     

}
